package com.x_memteor.base_master.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.x_memteor.base_master.application.MyApplication;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @author: Meteor
 * @description: 所有Fragment的基类
 * @version:
 * @date: 2016/12/28 0028 16:16
 * @company:
 * @email: lx802315@163.com
 */
public abstract class BaseFragment extends Fragment {

    //定义一个用于重复View的回收池
    private View contentView = null;

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible = false;
    // 标志位，标志Fragment已经初始化完成。
    public boolean isPrepared = false;
    /**
     * 标记已加载完成，保证懒加载只能加载一次
     */
    private boolean hasLoaded = false;

    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    private ProgressDialog dialog;
    protected Activity activity;
    //用于Butterknife 的绑定
    Unbinder unbinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView == null) {//判断回收池是否为空
            contentView = initLayout(inflater, container, false);
            unbinder = ButterKnife.bind(this, contentView);
            //初始化view的各控件
            isPrepared = true;
        }
        if (contentView != null) {
            unbinder = ButterKnife.bind(this, contentView);
            //初始化view的各控件
            isPrepared = true;
            return contentView;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 初始化Fragment的布局，当要创建试图时候调用
     *
     * @param inflater  布局填充器
     * @param container ViewGroup
     * @param b         标记
     * @return view 返回视图
     */
    protected abstract View initLayout(LayoutInflater inflater, ViewGroup container, boolean b);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        //初始化数据
        initData(savedInstanceState);
    }

    /**
     * 初始化数据，当ViewCreate被创建时调用此方法
     *
     * @param savedInstanceState
     */
    protected abstract void initData(Bundle savedInstanceState);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoadInit();
    }

    /**
     * 不可见
     */
    protected void onInvisible() {

    }

    /**
     * 懒加载方法，获取数据什么的放到这边来使用，在切换到这个界面时才进行网络请求
     */
    private void lazyLoadInit() {

        //如果该界面不对用户显示、已经加载、fragment还没有创建，
        //三种情况任意一种，不获取数据
        if (!isVisible || hasLoaded || !isPrepared) {
            return;
        }
        lazyLoad();
        hasLoaded = true;//注：关键步骤，确保数据只加载一次
    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();

    @Override
    public final void onDestroyView() {
        //移除当前视图，防止重复加载相同视图使得程序闪退
        ((ViewGroup) contentView.getParent()).removeView(contentView);
        super.onDestroyView();
        unbinder.unbind();
        isPrepared = false;
        hasLoaded = false;
    }

    /**
     * 判断用户是否登陆过
     *
     * @return true 为登陆成功 false 为没有登陆过
     */
//    protected boolean isLogin() {
//        return MyApplication.getInstance().getCurrentUser() != null;
//    }

    /**
     * 功能 ：显示一个警告对话框
     */
    protected void showAlertDialog(String title, String text) {
        if (builder == null) {
            //创建一个构建者对象
            builder = new AlertDialog.Builder(getContext());
            builder.setTitle(title).setMessage(text).setCancelable(false);
            builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //跳转到系统网络设置
                    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                    startActivity(intent);
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //退出虚拟机
                    System.exit(0);
                }
            });
        }
        alertDialog = builder.show();
    }

    /**
     * 功能:取消警告对话框
     */
    protected void dismissAlertDialog() {
        if (alertDialog != null) {
            //取消警告对话框
            alertDialog.dismiss();
        }
    }

    /**
     * 功能 ：显示一个进度条对话框
     */
    protected void showProcessDialog(String title, String msg, boolean falg) {
        if (dialog == null) {
            dialog = new ProgressDialog(getContext());
        }
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setCancelable(falg);
        dialog.show();
    }

    /**
     * 功能 ：取消一个进度条对话框
     */
    protected void dismissProcessDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

}
